﻿package com.site.controller;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.common.BaseController;
import com.common.CommonConstraint;
import com.entity.CustomerMessage;
import com.entity.MessageFile;
import com.my.MyTools;
import com.service.ContactItemsService;
import com.service.CustomerMessageService;
import com.service.ProductCategoryService;
import com.service.ProductionService;
import com.util.FileUtils;

@Controller
@RequestMapping("/")
public class MessageController extends BaseController {
	@Autowired
	private CustomerMessageService customerMessageService;
	@Autowired
	private ProductCategoryService productCategoryService;
	@Autowired
	private ProductionService productionService;
	@Autowired
	private ContactItemsService contactItemsService;
	@RequestMapping("/message")
	public String index(HttpServletRequest request){
		super.initCommon(request, productCategoryService, contactItemsService, productionService);
		return "site.message";
	}
	@RequestMapping("/message/post")
	public ModelAndView postMessage(HttpServletRequest request, HttpServletResponse response, CustomerMessage message){
		String msg = "";
		boolean result = false;
		if(MyTools.isEmpty(message.getMsgSubject())){
			msg = "请输入主题！";
		}else if(MyTools.isEmpty(message.getMsgEmail())){
			msg = "请输入邮箱！";
		}else if(MyTools.isEmpty(message.getMsgContent())){
			msg = "请输入内容！";
		}else{
			message.setSendTime(new Timestamp(System.currentTimeMillis()));
			message.setIsReply(false);
			msg = "很抱歉，由于内部原因发送消息失败！";
			MultipartHttpServletRequest multipartRequest;
			try{
				multipartRequest = (MultipartHttpServletRequest) request;
				result = saveMessage(message, multipartRequest);
			}catch(ClassCastException e){
				result = customerMessageService.postMessage(message);
			}
		}
		outJson("{success:" + result + ",msg:\"" + msg + "\"}", response);
		return null;
	}
	
	private boolean saveMessage(CustomerMessage message, MultipartHttpServletRequest request){
		System.out.println("表单上传了文件...");
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> files = multipartRequest.getFileMap();
		System.out.println("一共有" + files.size() + "个文件");
		Iterator<String> fileNames = multipartRequest.getFileNames();
		while(fileNames.hasNext()){
			String fileName = fileNames.next();
			System.out.println("文件名为：" + fileName);
			if(!MyTools.isEmpty(fileName)){
				CommonsMultipartFile file = (CommonsMultipartFile) files.get(fileName);
				System.out.println("原文件路径为：" + file.getOriginalFilename());
				String filePath = FileUtils.saveFile(file, request.getSession().getServletContext().getRealPath("/"), CommonConstraint.UPLOAD_PATH);
				System.out.println("保存文件路径为：" + filePath);
				MessageFile messageFile = new MessageFile();
				messageFile.setMessageFile(filePath);
				message.getMessageFiles().add(messageFile);
			}
		}
		return customerMessageService.postMessage(message);
	}
}
